﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using System.IO;
public partial class MSDS : SitePage
{
    public DocumentCollection lstResult = null;    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    protected override void OnInit(EventArgs e)
    {
        InitData();
        base.OnInit(e);
    }
    private void InitData()
    {
        this.pagerBottom.PageIndex=this.pagerTop.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = clsConfigData.DEFAULT_MSDS_PAGE_SIZE;
        this.Page.Header.Title = string.Format(Resources.Resource.TITLE, "MSDS");
    }
    private void LoadData()
    {
        int count = 0;
         Document objDocument = new Document();
        objDocument.CurrentPage = pagerTop.PageIndex;
        objDocument.OrderBy = "SortOrder";
        objDocument.OrderDirection = "ASC";
        objDocument.PageSize = pagerTop.PageSize;
        objDocument.Condition = " d.IsActive=1";        
        busDocument _busDocument = new busDocument();
        lstResult = _busDocument.List(objDocument);
        if (lstResult != null && lstResult.Count>0)
            count = lstResult.Count;
        pagerTop.SetPaging(count, objDocument.TotalRecords);
        pagerBottom.SetPaging(count, objDocument.TotalRecords);
    }
    public string GetSKU(int documentID)
    {
        string result = string.Empty;
        //get Product SKU
        busDocumentProduct busDocumentProduct = new busDocumentProduct();
        DocumentProductCollection lstProduct = busDocumentProduct.GetBydocumentID(documentID);
        if (lstProduct != null && lstProduct.Count > 0)
        {
            busProduct busProduct = new busProduct();
            Product objProduct = busProduct.GetBySKU(lstProduct[0].SKU);
            string link = "/product/" + clsText.ToSEO(objProduct.Name) + "/0/" + objProduct.ProductID;//
            result = "<a class='dLink' href='" + link + "'>" + lstProduct[0].SKU + "</a>";
        }
        else result = "&nbsp;";
        return result;
        
    }
    public string GetDocumentUrl(string filename)
    {
        string fullPath = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_DOCUMENT_FOLDER);
        if (File.Exists(fullPath + filename))
            return this.ApplicationRoot + clsConfigData.DEFAULT_UPLOAD_DOCUMENT_FOLDER + filename;
        return string.Empty;
    }
    protected void pagerTop_PageIndexChanging(object obj, CommonTagTime.PagerControl.PageIndexChangeEventArgs e)
    {
        this.pagerBottom.PageIndex = this.pagerTop.PageIndex = e.PageIndex;      
        LoadData();
    }
    protected void pagerTop_PageSizeChanging(object obj, CommonTagTime.PagerControl.PageSizeChangeEventArgs e)
    {
        this.pagerBottom.PageIndex = this.pagerTop.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = e.PageSize;
        LoadData();
    }
    
}